// pages/my/my.js
var httpTool = require('../../utils/network.js');
var util = require('../../utils/util.js')
const app = getApp()
Page({
  data:{
    userName:"",
    school:"",
    grade:"",
    major:"",
    provinceId:"",
    provinceName:"",
    cityId:"",
    cityName:"",
    districtId:"",
    address:"",
    districtName:"",
    returnAddress:"",
    memberPhone:"",
    memberId:"",
    idCard:"",
    vacationId:"",
    namingId:"",
    regionId:"",
    regionName:"",
  },
  onLoad:function(options){
    var memberId = "";
    var memberPhone="";
    if (app.userData.userId) {
      memberId = app.userData.userId;
      memberPhone = app.globalData.userInfo.phone;
    }else{
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }

    let namingId = "";
    let regionId = "";
    let regionName = "";
    let vacationId = "";
    if(options.namingId){
      namingId = options.namingId;
    }
    if(options.regionId) {
      regionId = options.regionId;
    }
    if(options.regionName) {
      regionName = options.regionName;
    }
    if(options.vacationId) {
      vacationId = options.vacationId;
    }
   
      // 生命周期函数--监听页面加载
    this.setData({
      namingId: namingId,
      regionId: regionId,
      regionName: regionName,
      vacationId: vacationId,
      memberId:memberId,
      memberPhone: memberPhone,
    })
  },
  onReady:function(){
    // 生命周期函数--监听页面初次渲染完成
    
  },
  onShow:function(){
      // 生命周期函数--监听页面显示
      var address = app.cityData.provinceName + app.cityData.cityName + app.cityData.countryName;
      let data = this.data;
      if(address){
          data.address = address;
          data.provinceId =app.cityData.provinceId;
          data.provinceName = app.cityData.provinceName;
          data.cityId = app.cityData.cityId;
          data.cityName = app.cityData.cityName;
          data.districtId = app.cityData.countryId;
          data.districtName = app.cityData.countryName;
      }
      if(app.schoolData.schoolName){
          data.school = app.schoolData.schoolName;
      }
      this.setData(data);
  },
  onHide:function(){
    // 生命周期函数--监听页面隐藏
    
  },
  onUnload:function(){
    // 生命周期函数--监听页面卸载
    app.cityData.provinceId = '';
    app.cityData.provinceName = '';
    app.cityData.cityId = '';
    app.cityData.cityName = '';
    app.cityData.countryId = '';
    app.cityData.countryName = '';
    app.schoolData.schoolName = '';
  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作
    
  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数
    
  },
  onShareAppMessage: function() {
    // 用户点击右上角分享
    // return {
    //   title: 'title', // 分享标题
    //   desc: 'desc', // 分享描述
    //   path: 'path' // 分享路径
    // }
  },
  toSelectCity:function(){
    util.navigateTo('/pages/city/city');
  },
  toSelectSchool: function () {
    util.navigateTo('/pages/school/school');
  },
  fieldListen:function(event) {
    let field = event.currentTarget.dataset['field'];
    let value = event.detail.value;
    console.log("field:" + field + ",value:" + value);
    let data = this.data;
    data[field] = value;
    this.setData(data)
  },
  assertSignUp:function(){
    var data = this.data;
    if(!data.userName){
      util.fxAlert("请输入姓名");
      return false;
    }
    if(!data.school){
      util.fxAlert("请选择学校");
      return false;
    }
    if(!data.major){
      util.fxAlert("请输入专业");
      return false;
    }
    if(!data.grade){
      util.fxAlert("请输入年级");
      return false;
    }
    if(!data.address){
      util.fxAlert("请选择返乡所在区域");
      return false;
    }
    if(!data.returnAddress){
      util.fxAlert("请输入详细地址");
      return false;
    }
    if(!data.idCard){
      util.fxAlert("请输入身份证号码");
      return false;
    }else{
      const regIdCard = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
      if (!regIdCard.test(data.idCard)) {
        util.fxAlert("身份证号码有误");
        return false;
      }
    }
    return true;
  },
  signUp: function () {
    var that = this
    var url = app.data.httpUrl + 'v1/enroll/saveOrUpdate'
    var param = that.data;
    console.log(param);
    if(!this.assertSignUp()){
      return;
    }
    httpTool.sendRequest(url, param, '提交中...', function (res) {
      util.fxAlert("报名成功！");
      wx.redirectTo({
        url:"/pages/index/index"
      })
    }, function (error) {
      util.fxAlert(error.msg);
    });
  },
})
